package pprz.components;

import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import pprz.data_components.Device;

public class DevicePanel extends Container
implements PropertyChangeListener, ActionListener {
	static final long serialVersionUID = 1;

	Device dev;
	Button init;
	
	public DevicePanel()
	{
		allocate();
		initialize();	
	}
	
	private void allocate() { dev=null; init=new Button("initialize"); }
	private void initialize() 
	{
		FlowLayout layout;
		layout=new FlowLayout();
		this.setLayout(layout);
//		init.setDefaultPaint(new GradientPaint(0,0,Color.white,0,1,Color.black,false));
//		init.setMouseOverPaint(new GradientPaint(0,0,Color.white.darker(),0,1,Color.black.brighter(),false));
//		init.setPushedPaint(Color.black);
		Label b;
		b=new Label("unknown"); //b.setDefaultPaint(Color.black);
		b.validate();
		this.add(b);
		TextArea port=new TextArea("unknown port", 5, 20, TextArea.SCROLLBARS_VERTICAL_ONLY);

		//port.setColumns(20);
		port.setEditable(false);
		this.add(port);
		
		
		//this.add(new LightButton("not ready"));
		this.add(init);
		this.setPreferredSize(layout.preferredLayoutSize(this));
		this.setSize(layout.preferredLayoutSize(this));
		this.setBackground(Color.green);
	}

	/* (non-Javadoc)
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent evt) {
		String property=evt.getPropertyName();
		if ("state".equals(property))
		{
			this.getComponent(0).setName((Boolean)(evt.getNewValue())?"ready":"not ready");
			if (Device.class.isAssignableFrom(evt.getSource().getClass())) 
				dev=(Device)evt.getSource();
		} else if ("port".equals(property))
		{
			((TextArea)this.getComponent(1)).setText((String)(evt.getNewValue()));
			((TextArea)this.getComponent(1)).setCaretPosition(100);
		}
		validate();
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(init) && dev!=null)
			dev.init();
	}
	
	
	
	
	
}
